package com.example.schoolblog.blog.Dto;

import lombok.Data;
import java.util.ArrayList;
import java.util.List;


@Data
public class PaginationDTO<T> {
    private List<T> data;
    private boolean showPrevious;
    private boolean showFirstPage;
    private boolean showNext;
    private boolean showEndPage;
    private Integer page;
    private Integer totalPage;
    private List<Integer> pages = new ArrayList<>();


    public void setPagination(int totalCount, Integer page, Integer size) {
        //总页数
        if(totalCount % size == 0){
            totalPage = totalCount / size;
        }else{
            totalPage = totalCount / size + 1;
        }
        //处理页数出界
        if(page < 1){
            page  = 1;
        }
        if(page > totalPage){
            page = totalPage;
        }
        this.page = page;
        //添加分页栏显示的页码
        pages.add(page);
        for(int i=1;i<=3;i++){
            if(page - i > 0){
                pages.add(0,page - i);
            }
            if(page + i <= totalPage){
                pages.add(page + i);
            }
        }
        //是否显示前一页
        if(page == 1){
            showPrevious = false;
        }else{
            showPrevious = true;
        }
        //是否显示最后一页
        if(page == totalPage){
            showNext = false;
        }else{
            showNext = true;
        }
        //是否显示跳转第一页
        if(pages.contains(1)){
            showFirstPage = false;
        }else{
            showFirstPage = true;
        }
        //是否显示跳转最后一页
        if(pages.contains(totalPage)){
            showEndPage = false;
        }else{
            showEndPage = true;
        }
    }
}
